package com.mrent.web.controller.manager;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

import com.mrent.entity.AdminShipping;
import com.mrent.entity.ManagerShipping;
import com.mrent.entity.OrderManager;
import com.mrent.service.AdminShippingService;
import com.mrent.service.ManagerShippingService;
import com.mrent.service.Page;
import com.mrent.util.Constance;
import com.mrent.util.RenderUtil;


@Controller
public class ManagerShippingController {
	
	@Autowired
	private ManagerShippingService managerShippingService;
	
	@Autowired
	private AdminShippingService adminShippingService;
	
	/**
	 * 支付管理.
	 * @return
	 */
	@RequestMapping("/manager/shipping/index")
	public String index(){
		
		return "manager/shipping/home";
	}
	
	/**
	 * 配送列表.
	 * @param session
	 * @param response
	 * @return
	 */
	@RequestMapping("/manager/shipping/list")
	public String list(String page,HttpSession session,HttpServletResponse response){
		int p = 1;
		Page pp = new Page();
		try{
			p = Integer.parseInt(page);
		}catch(Exception e){
			p = 1;
		}
		pp.setPageNo(p);
		pp.setPageSize(Constance.DEFAULT_PAGE_SIZE);
		OrderManager om = (OrderManager)session.getAttribute(Constance.SESSION_MANAGER_NAME);
		List<ManagerShipping> shippings ;
		if(om != null){
			shippings = managerShippingService.listShipping(om.getOrderMember(), pp);
			Map<String,Object> json = new HashMap<String,Object>();
			json.put("rows", shippings);
			json.put("total", pp.getTotalCount());
			RenderUtil.renderJson(json,response);
		}
		return null;
	}
	
	/**
	 * 店店添加配送方式.
	 * 
	 * @param payId
	 * @param response
	 * @return
	 */
	@RequestMapping("/manager/shipping/add/{shippingId}")
	public String add(@PathVariable Long shippingId,ModelMap model){
		if(shippingId!=null){
			AdminShipping shipping = adminShippingService.getShipping(shippingId);
			model.addAttribute("shipping", shipping);
		}
		return "manager/shipping/edit";
	}
	
	/**
	 * 店主保存配送方式.
	 * 
	 * @param managerShipping
	 * @param response
	 * @return
	 */
	@RequestMapping("/manager/shipping/save")
	public String save(ManagerShipping managerShipping,HttpServletResponse response,HttpSession session){
		OrderManager om = (OrderManager)session.getAttribute(Constance.SESSION_MANAGER_NAME);
		if(managerShipping.getId() != null && managerShipping.getId() >0){
			managerShipping.setManager(om.getOrderMember());
			managerShippingService.updateShipping(managerShipping);
		}else{
			managerShipping.setManager(om.getOrderMember());
			managerShippingService.saveShipping(managerShipping);
		}
		Map<String,Object> json = new HashMap<String,Object>();
		json.put("result", "Success");
		RenderUtil.renderJson(json, response);
		return null;
	}
	
	/**
	 * 店主删除他的配送方式.
	 * @param id
	 * @param response
	 * @return
	 */
	@RequestMapping("/manager/shipping/del/{id}")
	public String del(@PathVariable Long id,HttpServletResponse response){
		Map<String,Object> json = new HashMap<String,Object>();
		managerShippingService.delShipping(id);
		json.put("result", "Success");
		RenderUtil.renderJson(json, response);
		
		return null;
	}

}
